﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace ACNetMusicClient
{
    class Controller
    {
        private const int BUFFER_SIZE = 100;
        private const int PORT_NUMBER = 9999;
        private const string serverAddress = "127.0.0.1";

        static ASCIIEncoding encoding = new ASCIIEncoding();
        private TcpClient client = null;
        private Stream stream = null;
        private StreamReader reader = null;
        private StreamWriter writer = null;

        /**
         * initializing socket and connect 2 server, then initialize stream
         */
        public void connect()
        {
            try
            {
                client = new TcpClient();

                client.Connect(IPAddress.Parse(serverAddress), PORT_NUMBER);
                stream = client.GetStream();
                reader = new StreamReader(stream);
                writer = new StreamWriter(stream);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }                    
        }

        /**
         * check socket and stream and close them.
         */
        public  void disconnect()
        {
            try
            {
                if (client != null)
                {
                    client.Close();
                }
                // close all streams
                if (stream != null)
                {
                    stream.Close();
                    reader.Close();
                    writer.Close();
                }
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
        }
    }
}
